package com.tianyang.yukuaifu.business.auth.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.core.content.ContextCompat;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseEditActivity;
import com.tianyang.yukuaifu.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifu.business.auth.contract.AuthSelectTypeContract;
import com.tianyang.yukuaifu.business.auth.presenter.AuthSelectTypePresenter;
import com.tianyang.yukuaifu.business.main.bean.ChannelOutput;
import com.tianyang.yukuaifu.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.module.dialog.PosChannelDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifu.module.roundview.RoundLinearLayout;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：商户进件选择类型
 * Created by xyj on 2024/3/7
 */
public class AuthSelectTypeActivity extends BaseEditActivity implements AuthSelectTypeContract.View {

    @BindView(R.id.phoneGroupRll)
    RoundLinearLayout phoneGroupRll;
    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.merchantRll)
    RoundLinearLayout merchantRll;
    @BindView(R.id.individualRll)
    RoundLinearLayout individualRll;
    @BindView(R.id.companyRll)
    RoundLinearLayout companyRll;

    private PosChannelDialog vPosChannelDialog;

    private AuthSelectTypeContract.Presenter mPresenter;

    private String merchantType = AuthConfig.MCH_TYPE_SMALL;

    private boolean isMine;

    private String posChannel;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_auth_select_type;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("商户进件");
        mPresenter = new AuthSelectTypePresenter(this);
        isMine = getIntent().getBooleanExtra(AuthConfig.INTENT_IS_MINE, false);
        if (isMine) {
            phoneGroupRll.setVisibility(View.GONE);
        } else {
            phoneGroupRll.setVisibility(View.VISIBLE);
            mPresenter.getChannelList();
        }
    }

    @OnClick({R.id.merchantRll, R.id.individualRll, R.id.companyRll, R.id.confirmTv})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        switch (view.getId()) {
            case R.id.merchantRll:
                merchantRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fde6e6));
                individualRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fbfbfb));
                companyRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fbfbfb));
                merchantType = AuthConfig.MCH_TYPE_SMALL;
                break;
            case R.id.individualRll:
                merchantRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fbfbfb));
                individualRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fde6e6));
                companyRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fbfbfb));
                merchantType = AuthConfig.MCH_TYPE_INDIVIDUAL;
                break;
            case R.id.companyRll:
                merchantRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fbfbfb));
                individualRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fbfbfb));
                companyRll.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_fde6e6));
                merchantType = AuthConfig.MCH_TYPE_COMPANY;
                break;
            case R.id.confirmTv:
                String userName = "";
                UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
                if (output != null) {
                    userName = output.getUsername();
                }
                boolean isAgent = false;
                if (!isMine) {
                    if (TextUtils.isEmpty(phoneNumberEt.getText())) {
                        ToastUtil.show("商户手机号不能为空");
                        SoftInputUtil.openKeybord(phoneNumberEt, mContext);
                        return;
                    } else if (VerifyUtil.checkPhoneNumber(phoneNumberEt.getText())) {
                        ToastUtil.showTextViewPromptShort("商户手机号输入不正确");
                        SoftInputUtil.openKeybord(phoneNumberEt, mContext);
                        return;
                    }
                    userName = phoneNumberEt.getText().toString();
                    isAgent = true;
                }
                mPresenter.onStartAuthClick(merchantType, userName, isAgent, posChannel);
                break;
        }
    }

    @Override
    public void showChannelView(List<ChannelOutput> outputs) {
        if (outputs != null && !outputs.isEmpty()) {
            showPosChannelDialog(outputs);
        }
    }

    private void showPosChannelDialog(List<ChannelOutput> channelOutputs) {
        if (vPosChannelDialog == null) {
            vPosChannelDialog = new PosChannelDialog(getContext(), channelOutputs, false);
        }
        vPosChannelDialog.setCancelable(false);
        vPosChannelDialog.setCanceledOnTouchOutside(false);
        vPosChannelDialog.setListener(new ICallbackListener<ChannelOutput>() {
            @Override
            public void callback(ChannelOutput channelOutput) {
                if (channelOutput != null) {
                    posChannel = channelOutput.getCode();
                }
            }
        });
        vPosChannelDialog.show();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(AuthSelectTypeContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (vPosChannelDialog != null && vPosChannelDialog.isShowing()) {
            vPosChannelDialog.dismiss();
            vPosChannelDialog = null;
        }
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
